home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12525 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: RAND_MAX
  5. Date: 1 Apr 1996 06:55:43 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4joqpfINNg2s@keats.ugrad.cs.ubc.ca>
  8. References: <4jnr55$e6l@skivs.ski.org>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4jnr55$e6l@skivs.ski.org>, Gemini Thunder <gt@ns.oon.or.jp> wrote:
  12. >I have a (stupid) question / observation about RAND_MAX.
  13. >
  14. >K&R2 says:
  15. >  "rand returns a a pseudo-random integer in the range 0 to RAND_MAX,
  16. >which is at least 32767"
  17. >
  18. >It looks like RAND_MAX is not required to be the same as any other
  19. >limit (such as MAX_INT, etc.), that is fine, but why?  
  20. >Wouldn't it be easier if RAND_MAX == MAX_INT or some other value?  
  21. >
  22. >The reason I say this is how can you be sure your array will hold a
  23. >RAND_MAX sized integer, without some checking before hand, if RAND_MAX
  24. >is out there doing its own thing?
  25.  
  26. If you look at the prototype for rand(), the answer to that is obvious. It
  27. returns int, therefore you can store these numbers in arrays of int. The return
  28. type of rand() gives you the type of object in which you can store the result.
  29.  
  30. Presumably, the reason RAND_MAX is distinct from INT_MAX (not MAX_INT!!!) is
  31. to allow rand() the freedom to return a smaller range, say a few bits less than
  32. the size of an integer, which is still _stored_ in an int type.
  33.  
  34. >Or does ANSI pin it down (in which case why have a RAND_MAX #defined),
  35. >or am I just worked up over nothing and overlooking something obvious?
  36.  
  37. It pins it down in the #define for RAND_MAX. Often, RAND_MAX is just
  38.  
  39. #define RAND_MAX INT_MAX
  40.  
  41. but it need not be so, hence the separate symbolic constants.
  42. -- 
  43.  
  44.